/**
 * e-fuli.com Inc.
 * Copyright (c) 2015-2018 All Rights Reserved.
 */
package ltd.newbee.mall.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * <pre>
 * 日期工具类
 * </pre>
 * @author jimmy
 * @version $Id: DateUtil.java, v 0.1 2017年12月15日 上午11:32:16 jimmy Exp $
 */
public final class DateUtil {

    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_MINUTE_PATTERN = "yyyy-MM-dd HH:mm";
    public static final String DATE_MINUTE_NO_YEAR_PATTERN = "M月d日 HH:mm";
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    public static final String DATE_PATTERN_SPOT = "yyyy.MM.dd";
    public static final String DATE_PATTERN_SPOT_SHORT = "yy.MM.dd";
    public static final String MONTH_PATTERN = "yyyy-MM";
    public static final String SHORT_DATE_TIME_PATTERN = "yyyyMMddHHmmss";
    public static final String SHORT_DATE_PATTERN = "yyyyMMdd";
    public static final String DATE_TIME_PATTERN_MILL = "yyyyMMddHHmmssSSS";
    public static final String YEAR_MONTH_PATTERN = "yyyyMM";
    public static final String ONLY_TIME_SHORT_PATTERN = "HHmmss";
    public static final String ONLY_TIME_LONG_PATTERN = "HH:mm:ss";
    public static final String DATE_TIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss SSS";
    public static final String SHORT_DATE_PATTERN_CHINESE = "yyyy年MM月dd日";
    public static final String DATE_TIME_PATTERN_CHINESE = "yyyy年MM月dd日 HH:mm:ss";
    public static final String CRON_TIME_PATTERN = "ss mm HH dd MM ? yyyy";
    public static final String CST_TIME_PATTERN = "EEE MMM dd HH:mm:ss zzz yyyy"; // Sat May 06 14:07:41 CST

    private DateUtil() {

    }

    public static void validateTimeString(final String dateTimeString, final String pattern) throws RuntimeException {
        try {
            dateParse(dateTimeString, pattern);
        } catch (Exception e) {
            throw new RuntimeException(dateTimeString + "日期格式错误");
        }
    }

    /**
     * <pre>
     * 获取时间上限(21世纪最后一天最后一秒)14位字符串
     * </pre>
     * @return
     */
    public static String getMaxShortDateTime() {
        return "20991231235959";
    }

    /**
     * <pre>
     * 获取当前时间的14位字符串
     * </pre>
     * @return
     */
    public static String getCurrentShortDateTime() {
        return shortDateTimeFormat(new Date());
    }

    /**
     * <pre>
     * 获取当前时间的14位字符串mill
     * </pre>
     * @return
     */
    public static String getCurrentShortDateTimeMill() {
        return shortDateTimeFormatMill(new Date());
    }

    /**
     * <pre>
     * 获取当前日期的8位字符串
     * </pre>
     * @return
     */
    public static String getCurrentShortDate() {
        return shortDateFormat(new Date());
    }

    /**
     * <pre>
     * 将日期时间格式化成yyyyMMddHHmmss
     * </pre>
     * @param date
     * @return
     */
    public static String shortDateTimeFormat(Date date) {
        return dateFormat(date, DateUtil.SHORT_DATE_TIME_PATTERN);
    }

    /**
     * <pre>
     * 将日期时间格式化成yyyyMMddHHmmss
     * </pre>
     * @param date
     * @return
     */
    public static String shortDateTimeFormatMill(Date date) {
        return dateFormat(date, DateUtil.DATE_TIME_PATTERN_MILL);
    }

    /**
     * <pre>
     * 将日期格式化成yyyyMMdd
     * </pre>
     * @param date
     * @return
     */
    public static String shortDateFormat(Date date) {
        return dateFormat(date, DateUtil.SHORT_DATE_PATTERN);
    }

    /**
     * <pre>
     * 将字符串日期时间yyyyMMddHHmmss转成Date对象
     * </pre>
     * @param dateTimeString
     * @return
     */
    public static Date shortDateTimeParse(String dateTimeString) {
        return dateParse(dateTimeString, DateUtil.SHORT_DATE_TIME_PATTERN);
    }

    /**
     * <pre>
     * 将字符串日期yyyyMMdd转成Date对象
     * </pre>
     * @param dateString
     * @return
     */
    public static Date shortDateParse(String dateString) {
        return dateParse(dateString, DateUtil.SHORT_DATE_PATTERN);
    }

    /**
     * <pre>
     * 长时间字符串转短时间字符串
     * </pre>
     * @param longDateTime
     * @return
     */
    public static String longDateTimeToShortDateTime(String longDateTime) {
        return shortDateTimeFormat(dateParse(longDateTime, DateUtil.DATE_TIME_PATTERN));
    }

    /**
     * <pre>
     * 长日期字符串转短日期字符串
     * </pre>
     * @param longDate
     * @return
     */
    public static String longDateToShortDate(String longDate) {
        return shortDateFormat(dateParse(longDate, DateUtil.DATE_PATTERN));
    }

    /**
     * <pre>
     * 短时间字符串转长时间字符串
     * </pre>
     * @param shortDateTime
     * @return
     */
    public static String shortDateTimeToLongDateTime(String shortDateTime) {
        return dateFormat(dateParse(shortDateTime, DateUtil.SHORT_DATE_TIME_PATTERN), DateUtil.DATE_TIME_PATTERN);
    }

    /**
     * <pre>
     * 短时间字符串转中文时间
     * </pre>
     * @param shortDate
     * @return
     */
    public static String shortDateToChineseDate(String shortDate) {
        return dateFormat(dateParse(shortDate, DateUtil.SHORT_DATE_PATTERN), DateUtil.SHORT_DATE_PATTERN_CHINESE);
    }

    /**
     * <pre>
     * 短时间字符串转带.时间
     * </pre>
     * @param shortDate
     * @return
     */
    public static String shortDateToDianDate(String shortDate) {
        return dateFormat(dateParse(shortDate, DateUtil.SHORT_DATE_PATTERN), DateUtil.DATE_PATTERN_SPOT);
    }

    /**
     * <pre>
     * 短时间字符串转长时间字符串
     * </pre>
     * @param shortDateTime
     * @return
     */
    public static String shortDateTimeToLongDateMinute(String shortDateTime) {
        return dateFormat(dateParse(shortDateTime, DateUtil.SHORT_DATE_TIME_PATTERN), DateUtil.DATE_MINUTE_PATTERN);
    }

    public static String shortDateTimeToDateMinute(String shortDateTime) {
        return dateFormat(dateParse(shortDateTime, DateUtil.SHORT_DATE_TIME_PATTERN), DateUtil.DATE_MINUTE_NO_YEAR_PATTERN);
    }

    /**
     * <pre>
     * 短日期字符串转长日期字符串
     * </pre>
     * @param shortDate
     * @return
     */
    public static String shortDateToLongDate(String shortDate) {
        return dateFormat(dateParse(shortDate, DateUtil.SHORT_DATE_PATTERN), DateUtil.DATE_PATTERN);
    }

    /**
     * <pre>
     * 短日期字符串转月和天字符串，MM.dd
     * </pre>
     * @param shortDate
     * @return
     */
    public static String shortDateToMonthDay(String shortDate) {
        return dateFormat(dateParse(shortDate, DateUtil.SHORT_DATE_PATTERN), DateUtil.DATE_PATTERN_SPOT_SHORT);
    }

    /**
     * 日期相加减天数
     * @param date
     *            如果为Null，则为当前时间
     * @param days
     *            加减天数
     * @param includeTime
     *            是否包括时分秒,true表示包含
     * @return
     * @throws ParseException
     */
    public static Date dateAdd(Date date, int days, boolean includeTime) {
        if (date == null) {
            date = new Date();
        }
        if (!includeTime) {
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_PATTERN);
            try {
                date = sdf.parse(sdf.format(date));
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    /**
     * 时间格式化成字符串
     * @param date
     *            Date
     * @param pattern
     *            StringUtils.DATE_TIME_PATTERN || StringUtils.DATE_PATTERN， 如果为空，则为yyyy-MM-dd
     * @return
     */
    public static String dateFormat(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        if (StringUtil.isBlank(pattern)) {
            pattern = DateUtil.DATE_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String dateFormatChinese(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        if (StringUtil.isBlank(pattern)) {
            pattern = DateUtil.DATE_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.CHINA);
        return sdf.format(date);
    }

    /**
     * 字符串解析成时间对象
     * @param dateTimeString
     *            String
     * @param pattern
     *            StringUtils.DATE_TIME_PATTERN || StringUtils.DATE_PATTERN，如果为空，则为yyyy-MM-dd
     * @return
     */
    public static Date dateParse(String dateTimeString, String pattern) {
        if (StringUtil.isBlank(pattern)) {
            pattern = DateUtil.DATE_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        try {
            return sdf.parse(dateTimeString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Date dateParse(String dateTimeString, String pattern, Locale locale) {
        if (StringUtil.isBlank(pattern)) {
            pattern = DateUtil.DATE_PATTERN;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, locale);
        try {
            return sdf.parse(dateTimeString);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String dateParse(String dateString, int days) {
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_DATE_PATTERN);
        try {
            Date parse = sdf.parse(dateString);
            return shortDateFormat(dateAddDays(parse, days));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String dateParseMonth(String dateString, int month) {
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_DATE_PATTERN);
        try {
            Date parse = sdf.parse(dateString);
            return shortDateFormat(dateAddMonths(parse, month));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static String dateParseYear(String dateString, int year) {
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_DATE_PATTERN);
        try {
            Date parse = sdf.parse(dateString);
            return shortDateFormat(dateAddYears(parse, year));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 将日期时间格式成只有日期的字符串（可以直接使用dateFormat，Pattern为Null进行格式化）
     * @param dateTime
     *            Date
     * @return
     */
    public static String dateTimeToDateString(Date dateTime) {
        String dateTimeString = DateUtil.dateFormat(dateTime, DateUtil.DATE_TIME_PATTERN);
        return dateTimeString.substring(0, 10);
    }

    public static String dateTimeToDateTimeString(Date dateTime) {
        String dateTimeString = DateUtil.dateFormat(dateTime, DateUtil.SHORT_DATE_TIME_PATTERN);
        return dateTimeString;
    }

    /**
     * 当时、分、秒为00:00:00时，将日期时间格式成只有日期的字符串， 当时、分、秒不为00:00:00时，直接返回
     * @param dateTime
     *            Date
     * @return
     */
    public static String dateTimeToDateStringIfTimeEndZero(Date dateTime) {
        String dateTimeString = DateUtil.dateFormat(dateTime, DateUtil.DATE_TIME_PATTERN);
        if (dateTimeString.endsWith("00:00:00")) {
            return dateTimeString.substring(0, 10);
        } else {
            return dateTimeString;
        }
    }

    /**
     * 将日期时间格式成日期对象，和dateParse互用
     * @param dateTime
     *            Date
     * @return Date
     */
    public static Date dateTimeToDate(Date dateTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(dateTime);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 时间加减小时
     * @param startDate
     *            要处理的时间，Null则为当前时间
     * @param hours
     *            加减的小时
     * @return Date
     */
    public static Date dateAddHours(Date startDate, int hours) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.HOUR, c.get(Calendar.HOUR) + hours);
        return c.getTime();
    }

    /**
     * 时间加减分钟
     * @param startDate
     *            要处理的时间，Null则为当前时间
     * @param minutes
     *            加减的分钟
     * @return
     */
    public static Date dateAddMinutes(Date startDate, int minutes) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + minutes);
        return c.getTime();
    }

    /**
     * 时间加减秒数
     * @param startDate
     *            要处理的时间，Null则为当前时间
     * @param seconds
     *            加减的秒数
     * @return
     */
    public static Date dateAddSeconds(Date startDate, int seconds) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.SECOND, c.get(Calendar.SECOND) + seconds);
        return c.getTime();
    }

    /**
     * 时间加减天数
     * @param startDate
     *            要处理的时间，Null则为当前时间
     * @param days
     *            加减的天数
     * @return Date
     */
    public static Date dateAddDays(Date startDate, int days) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.DATE, c.get(Calendar.DATE) + days);
        return c.getTime();
    }

    /**
     * 时间加减月数
     * @param startDate
     *            要处理的时间，Null则为当前时间
     * @param months
     *            加减的月数
     * @return Date
     */
    public static Date dateAddMonths(Date startDate, int months) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.add(Calendar.MONTH, months);
        // c.set(Calendar.MONTH, c.get(Calendar.MONTH) + months);
        return c.getTime();
    }

    /**
     * 时间加减年数
     * @param startDate
     *            要处理的时间，Null则为当前时间
     * @param years
     *            加减的年数
     * @return Date
     */
    public static Date dateAddYears(Date startDate, int years) {
        if (startDate == null) {
            startDate = new Date();
        }
        Calendar c = Calendar.getInstance();
        c.setTime(startDate);
        c.set(Calendar.YEAR, c.get(Calendar.YEAR) + years);
        return c.getTime();
    }

    /**
     * 时间比较（如果myDate>compareDate返回1，<返回-1，相等返回0）
     * @param myDate
     *            时间
     * @param compareDate
     *            要比较的时间
     * @return int
     */
    public static int dateCompare(Date myDate, Date compareDate) {
        Calendar myCal = Calendar.getInstance();
        Calendar compareCal = Calendar.getInstance();
        myCal.setTime(myDate);
        compareCal.setTime(compareDate);
        return myCal.compareTo(compareCal);
    }

    /**
     * 获取两个时间中最小的一个时间
     * @param date
     * @param compareDate
     * @return
     */
    public static Date dateMin(Date date, Date compareDate) {
        if (date == null) {
            return compareDate;
        }
        if (compareDate == null) {
            return date;
        }
        if (1 == dateCompare(date, compareDate)) {
            return compareDate;
        } else if (-1 == dateCompare(date, compareDate)) {
            return date;
        }
        return date;
    }

    /**
     * 获取两个时间中最大的一个时间
     * @param date
     * @param compareDate
     * @return
     */
    public static Date dateMax(Date date, Date compareDate) {
        if (date == null) {
            return compareDate;
        }
        if (compareDate == null) {
            return date;
        }
        if (1 == dateCompare(date, compareDate)) {
            return date;
        } else if (-1 == dateCompare(date, compareDate)) {
            return compareDate;
        }
        return date;
    }

    /**
     * 获取两个日期（不含时分秒）相差的天数，不包含今天
     * @param startDate
     * @param endDate
     * @return
     */
    public static int dateBetween(Date startDate, Date endDate) {
        Date dateStart = dateParse(dateFormat(startDate, DATE_PATTERN), DATE_PATTERN);
        Date dateEnd = dateParse(dateFormat(endDate, DATE_PATTERN), DATE_PATTERN);
        return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000 / 60 / 60 / 24);
    }

    /**
     * 获取两个日期秒
     * @param startDate
     * @param endDate
     * @return
     */
    public static int dateBetweenSeconds(Date startDate, Date endDate) {
        Date dateStart = dateParse(dateFormat(startDate, SHORT_DATE_TIME_PATTERN), SHORT_DATE_TIME_PATTERN);
        Date dateEnd = dateParse(dateFormat(endDate, SHORT_DATE_TIME_PATTERN), SHORT_DATE_TIME_PATTERN);
        return (int) ((dateEnd.getTime() - dateStart.getTime()) / 1000);
    }

    /**
     * 获取两个日期（不含时分秒）相差的天数，包含今天
     * @param startDate
     * @param endDate
     * @return
     */
    public static int dateBetweenIncludeToday(Date startDate, Date endDate) {
        return dateBetween(startDate, endDate) + 1;
    }

    /**
     * 获取日期时间的年份，如2017-02-13，返回2017
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    /**
     * 获取日期时间的月份，如2017年2月13日，返回2
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取日期时间的第几天（即返回日期的dd），如2017-02-13，返回13
     * @param date
     * @return
     */
    public static int getDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DATE);
    }

    /**
     * 获取日期时间中这周的第几天，如周末返回1，周三返回4，周六返回7
     * @param date
     * @return
     */
    public static int getDayOfWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 获取日期时间当月的总天数，如2017-02-13，返回28
     * @param date
     * @return
     */
    public static int getDaysOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getActualMaximum(Calendar.DATE);
    }

    /**
     * 获取日期时间当月的天数，如2017-02-13，返回13
     * @param date
     * @return
     */
    public static int getDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取日期时间当年的总天数，如2017-02-13，返回2017年的总天数
     * @param date
     * @return
     */
    public static int getDaysOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
    }

    public static int getWeekOfYear(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 根据时间获取当月最大的日期
     * <li>2017-02-13，返回2017-02-28</li>
     * <li>2016-02-13，返回2016-02-29</li>
     * <li>2016-01-11，返回2016-01-31</li>
     * @param date
     *            Date
     * @return
     */
    public static Date maxDateOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int value = cal.getActualMaximum(Calendar.DATE);
        return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
    }

    /**
     * 根据时间获取当月最小的日期，也就是返回当月的1号日期对象
     * @param date
     *            Date
     * @return
     */
    public static Date minDateOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int value = cal.getActualMinimum(Calendar.DATE);
        return dateParse(dateFormat(date, MONTH_PATTERN) + "-" + value, null);
    }

    /**
     * 判断是不是工作日，周一到周五返回true，周六周末返回false
     * @param date
     * @return
     */
    public static boolean isWorkDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        if (day == 1 || day == 7) {
            return false;
        }
        return true;
    }

    /**
     * 秒转成天(四舍五入)
     * @param seconds
     * @return
     */
    public static int secondsToDays(int seconds) {
        final float divisor = 3600 * 24;
        return Math.round(seconds / divisor);
    }

    /**
     * <pre>
     * 获取昨天的日期 格式yyyyMMdd
     * </pre>
     * @return
     */
    public static String getYesterday() {
        return shortDateFormat(dateAdd(new Date(), -1, false));
    }

    public static String getMonthStartDate() {
        return getCurrentShortDate().substring(0, 6) + "01";
    }

    public static String getYearStartDate() {
        return getCurrentShortDate().substring(0, 4) + "0101";
    }

    /**
     * <pre>
     * 获取指定的日期 格式yyyyMMdd
     * </pre>
     * @param day
     *            1表示明天，-1表示昨天
     * @return
     */
    public static String getAppointDay(int day) {
        return shortDateFormat(dateAdd(new Date(), day, false));
    }

    /**
     * <pre>
     * 获取指定的日期 格式yyyyMM
     * </pre>
     * @param month
     *            1表示下一个月，-1表示上一个月
     * @return
     */
    public static String getAppointMonth(int month) {
        return dateFormat(dateAddMonths(new Date(), month), YEAR_MONTH_PATTERN);
    }

    /**
     * <pre>
     * 获取开始时间 格式yyyyMMddHHmmss
     * </pre>
     * @return
     */
    public static String getStartTime(String startDate) {
        return startDate + "000000";
    }

    /**
     * <pre>
     * 获取开始时间 格式yyyyMMddHHmmss
     * </pre>
     * @return
     */
    public static String getEndTime(String endDate) {
        return endDate + "235959";
    }

    /**
     * <pre>
     * 获取开始时间 格式yyyyMMddHHmmss
     * </pre>
     * @return
     */
    public static String getStartTimeHour(String startHour) {
        return startHour + "0000";
    }

    /**
     * <pre>
     * 获取开始时间 格式yyyyMMddHHmmss
     * </pre>
     * @return
     */
    public static String getEndTimeHour(String endHour) {
        return endHour + "5959";
    }

    public static String getDateString(String shortDate, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(SHORT_DATE_PATTERN);
        try {
            Date dateResult = sdf.parse(shortDate);
            sdf = new SimpleDateFormat(dateFormat);
            return sdf.format(dateResult);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * <pre>
     * 获取指定日期的月份最大天数
     * </pre>
     * @param date
     * @return
     */
    public static int getDaysOfMonth(String date) {
        if (StringUtil.isEmpty(date)) {
            return 0;
        }
        int month = Integer.parseInt(date.substring(4, 6));
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.YEAR, Integer.parseInt(date.substring(0, 4)));
        ca.set(Calendar.MONTH, month - 1);
        int maxDays = ca.getActualMaximum(Calendar.DAY_OF_MONTH);
        return maxDays;
    }

    /**
     * 返回两个日期相差的时间
     * @param startTime
     * @param endTime
     * @param flag
     *            两个日期相差的时间 1：天，2：小时，3：分钟，4：秒数，5，毫秒数
     * @return
     */
    public static long dateDiff(long startTime, long endTime, int flag) {
        // 按照传入的格式生成一个simpledateformate对象F
        long nd = 1000 * 24 * 60 * 60; // 一天的毫秒数
        long nh = 1000 * 60 * 60; // 一小时的毫秒数
        long nm = 1000 * 60; // 一分钟的毫秒数
        long ns = 1000; // 一秒钟的毫秒数long diff;
        // 获得两个时间的毫秒时间差异
        long diff = endTime - startTime;
        switch (flag) {
        case 1:
            diff = diff / nd; // 计算差多少天
            break;
        case 2:
            diff = diff / nh; // 计算差多少小时
            break;
        case 3:
            diff = diff / nm; // 计算差多少分钟
            break;
        case 4:
            diff = diff / ns; // 计算差多少秒
            break;

        default:
            break;
        }
        return diff;
    }

    /**
     * long型的时间转化成14位的年月日时分秒
     * @param longTime
     * @return
     */
    public static String longTypeTimeToStringType(Long longTime) {
        Date date = new Date(longTime);
        return dateFormat(date, SHORT_DATE_TIME_PATTERN);
    }

    // 获取本周周一的时间
    public static Date getThisWeekMonday() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        // 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    public static void main(String[] args) {
        // String endTime = DateUtil.dateFormat(new Date(), DateUtil.CRON_TIME_PATTERN);
        String startTime = "20180425";
        String endTime = "20211215";
        // System.out.println(DateUtil.dateBetweenIncludeToday(DateUtil.shortDateParse(startTime), DateUtil.shortDateParse(endTime)));
        System.out.println(getDayOfWeek(new Date()));
    }
}
